/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.worlds.Main;
import net.worlds.MenuView;
import net.worlds.Util;

/**
 *
 * @author Jacob
 */
public class ChatUserPanel extends EFrame{
    private ChatUserUpdater updater;
    private final List<String> users = Collections.synchronizedList(new ArrayList<String>());
    public ChatUserPanel()
    {
        super(10, 250, 200, 200);
        setUndecorated(true);
        setBackgroundColor(shadowColor);
    }
    @Override
    public void render(Graphics2D g)
    {
        super.render(g);
        int y = getY() + 16;
        g.setColor(Color.white);
        synchronized(users)
        {
            for(String user : users)
            {
                g.drawString(user, getX() + 5, y);
                y += lineHeight;
            }
        }
    }
    @Override
    public void setVisible(boolean v)
    {
        super.setVisible(v);
        if(v)
        {
            updater = new ChatUserUpdater();
            new Thread(updater).start();
        }
        else if(updater != null)
        {
            updater.running = false;
            updater = null;
        }
    }
    class ChatUserUpdater implements Runnable {
        public boolean running = true;
        public void run() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
            }
            while(Main.getView() instanceof MenuView)
            {
                String result = Util.httpGet("http://"+Main.siteURL+"/frames/getChatUsers.php");
                synchronized(users)
                {
                    users.clear();
                    for(String name : result.split(","))
                    {
                        users.add(name);
                    }
                }
                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException ex) {
                }
            }
        }
    }
}
